package com.example.administrator.runbook.fragments.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.fragments.community.model.Tweets;
import com.example.administrator.runbook.fragments.community.utils.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */

public class HotListAdapter extends BaseAdapter {

    private List<Tweets> list;
    private Context context;
    private LayoutInflater inflater;

    public HotListAdapter(Context context, List<Tweets> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hot_fragment, parent, false);
            convertView.setTag(new ViewHold(convertView));
        }
        ViewHold hold = (ViewHold) convertView.getTag();
        Tweets tweets = list.get(position);
        hold.bindView(position, tweets);
        return convertView;
    }

    private static class ViewHold {

        private ImageView imageView;
        private TextView textName;
        private TextView textLv;
        private TextView textDate;
        private TextView textTitle;
        private TextView textContext;
        private TextView textComment;
        private TextView textShare;

        public ViewHold(View itemView) {
            imageView = (ImageView) itemView.findViewById(R.id.image_hot_fragment);
            textName = (TextView) itemView.findViewById(R.id.name_hot_fragment);
            textLv = (TextView) itemView.findViewById(R.id.lv_hot_fragment);
            textDate = (TextView) itemView.findViewById(R.id.date_hot_fragment);
            textTitle = (TextView) itemView.findViewById(R.id.title_hot_fragment);
            textContext = (TextView) itemView.findViewById(R.id.context_hot_fragment);
            textComment = (TextView) itemView.findViewById(R.id.comment_hot_fragment);
            textShare = (TextView) itemView.findViewById(R.id.share_hot_fragment);
        }

        public void bindView(int position, Tweets data) {

            Picasso.with(imageView.getContext())
                    .load(data.getAvatar())
                    .transform(new CircleTransform())
                    .placeholder(R.mipmap.avatar_default)
                    .resizeDimen(R.dimen.avatar_size, R.dimen.avatar_size)
                    .error(R.mipmap.avatar_default)
                    .into(imageView);
            textName.setText(data.getNickname());
            textLv.setText("lv." + data.getLv());
            textDate.setText(data.getData(data.getCreated()));
            textTitle.setText(data.getTitle());
            textContext.setText(data.getContent());
            textComment.setText(data.getCommented());
            textShare.setText(data.getRetweeted());
        }
    }

}
